<?php 

function isEchoQuery() {
	return false;
}

function isLogActive() {
	return false;
}

function doLog($msg) {
	if (isLogActive()) {
		echo $msg;
	}
}

function ejecutarQuery($query) {
	include('Connections/congress.php'); 
	mysql_select_db($database_congress, $congress);
	$query_resultado = $query;
	$resultado = mysql_query($query_resultado, $congress) or die(mysql_error());
	$row_resultado = mysql_fetch_assoc($resultado);
	
	if (isEchoQuery()) {
		echo $query . "<br />";
	}
	
	return $row_resultado;
}

function obtenerListado($query) {
	include('Connections/congress.php'); 
	mysql_select_db($database_congress, $congress);
	$query_resultado = $query;
	$resultado = mysql_query($query_resultado, $congress) or die(mysql_error());
	
	if (isEchoQuery()) {
		echo $query . "<br />";
	}
	
	return $resultado;
}

function executeUpdateQuery($query) {
	include('Connections/congress.php'); 
	mysql_select_db($database_congress, $congress);
	$query_resultado = $query;
	$resultado = mysql_query($query_resultado, $congress) or die(mysql_error());

	if (isEchoQuery()) {
		echo $query . "<br />";
	}

}

function obtenerCantidadLineas($query) {
	include('Connections/congress.php'); 
	mysql_select_db($database_congress, $congress);
	$query_resultado = $query;
	$resultado = mysql_query($query_resultado, $congress) or die(mysql_error());
	$row_resultado = mysql_fetch_assoc($resultado);
	
	if (isEchoQuery()) {
		echo $query . "<br />";
	}
	
	return mysql_num_rows($resultado);
}

function getNextValSeq($seqName) {

	include('Connections/congress.php'); 

	mysql_select_db($database_congress, $congress);
	$query_nextValeSeq = sprintf("SELECT * FROM sequence where seq_name = '%s'", $seqName);
	$nextValeSeq = mysql_query($query_nextValeSeq, $congress) or die(mysql_error());
	$row_nextValeSeq = mysql_fetch_assoc($nextValeSeq);
	$totalRows_nextValeSeq = mysql_num_rows($nextValeSeq);
	
	$nextVal = $row_nextValeSeq['seq_value'];
	
	$query_updateValeSeq = sprintf("UPDATE sequence set seq_value = %s where seq_name = '%s'", $nextVal + 1, $seqName);
	$updateValeSeq = mysql_query($query_updateValeSeq, $congress) or die(mysql_error());
	
	mysql_free_result($nextValeSeq);
	
	return $nextVal;

}

function getValor($param) {
	if (isset($_GET[$param])) {
		return $_GET[$param];
	} else if (isset($_POST[$param])) {
		return $_POST[$param];
	}
	return "";
	
}

function getValorReg($param, $registro) {
	if (isset($_GET[$param])) {
		return $_GET[$param];
	} else if (isset($_POST[$param])) {
		return $_POST[$param];
	}
	return $registro[$param];

}

function is_valid_email($email) {
  $result = TRUE;
  if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email)) {
    $result = FALSE;
  }
  return $result;
}

function obtenerCongress($idCongress) {
	$query = "SELECT * FROM CONGRESS WHERE ID_CONGRESS = " . $idCongress;
	return ejecutarQuery($query);
}

function obtenerSessionsPorCongress($idCongress) {
	$query = "SELECT * FROM SESSION WHERE ID_CONGRESS = " . $idCongress . " ORDER BY NAME";
	return obtenerListado($query);
}

function getAllCongress() {
	$query = "SELECT * FROM CONGRESS WHERE DELETED = 0";
	return obtenerListado($query);
}

function updateShowAbstract($idAbstract, $show) {
	$query = "UPDATE ABSTRACT SET SHOW_ABSTRACT = " . $show . " WHERE ID_ABSTRACT = " . $idAbstract;
	executeUpdateQuery($query);
}

function obtenerSessionsPorCongressYNombre($idCongress, $nombre) {
	$query = "SELECT * FROM SESSION WHERE ID_CONGRESS = " . $idCongress . " AND NAME LIKE '%" . $nombre . "%' ORDER BY NAME";
	return obtenerListado($query);
}

function obtenerAbstractsPorSession($idSession) {
	$query = "SELECT * FROM ABSTRACT WHERE DELETED = 0 AND ID_SESSION = " . $idSession . " ORDER BY TITLE";
	return obtenerListado($query);
}

function obtenerCantidadSesionsPorCongress($idCongress) {
	$query = "SELECT * FROM SESSION WHERE ID_CONGRESS = " . $idCongress . " ORDER BY NAME";
	return obtenerCantidadLineas($query);
}

function obtenerCantidadSesionsPorCongressYNombre($idCongress, $nombre) {
	$query = "SELECT * FROM SESSION WHERE ID_CONGRESS = ". $idCongress ." AND NAME LIKE '%".$nombre."%'";
	return obtenerCantidadLineas($query);
}

function obtenerCantidadAbstractsPorSession($idSession) {
	$query = "SELECT * FROM ABSTRACT WHERE DELETED = 0 AND ID_SESSION = " . $idSession;
	return obtenerCantidadLineas($query);
}

function obtenerAbstractsPorCongress($idCongress) {
	$query = sprintf("SELECT * FROM ABSTRACT WHERE DELETED = 0 AND ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE S.ID_CONGRESS = %s ) ORDER BY TITLE ", $idCongress);
	return obtenerListado($query);
}

function obtenerAbstractsPorCongressYNombre($idCongress, $nombre) {
	$query = "SELECT * FROM ABSTRACT WHERE DELETED = 0 AND TITLE LIKE '%" . $nombre . "%' AND DELETED = 0 AND ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE S.ID_CONGRESS = " . $idCongress . ") ORDER BY TITLE ";
	return obtenerListado($query);
}


function obtenerCantidadAbstractsPorCongress($idCongress) {
	$query = sprintf("SELECT * FROM ABSTRACT WHERE DELETED = 0 AND ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE S.ID_CONGRESS = %s) ", $idCongress);
	return obtenerCantidadLineas($query);
}

function obtenerCantidadAbstractsPorCongressYNombre($idCongress, $nombre) {
	$query = "SELECT * FROM ABSTRACT WHERE DELETED = 0 AND TITLE LIKE '%" . $nombre . "%' AND DELETED = 0 AND ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE S.ID_CONGRESS = " . $idCongress . ")";
	return obtenerCantidadLineas($query);
}


function obtenerAbstractPorIdAbstract($idAbstract) {
	$query = "SELECT * FROM ABSTRACT WHERE ID_ABSTRACT = " . $idAbstract;
	return ejecutarQuery($query);
}

function validarKeyCongress($key, $idcongress) {
	$congress = obtenerCongress($idcongress);
	return $congress['LOGIN_KEY'] == $key;
}

function obtenerAuthorsPorCongressYNombre($idCongress, $search, $start) {

	if ($start == 'true') {
		$search = $search . "%";
	} else {
		$search = "%" . $search . "%";
	}

	$query = "SELECT * FROM AUTHOR WHERE ID_CONGRESS = " . $idCongress . " AND NAME LIKE '" . $search . "' ORDER BY NAME;";

	return obtenerListado($query);

}

function obtenerCantidadAuthorsPorCongressYNombre($idCongress, $search, $start) {

	if ($start == 'true') {
		$search = $search . "%";
	} else {
		$search = "%" . $search . "%";
	}

	$query = "SELECT * FROM AUTHOR WHERE ID_CONGRESS = " . $idCongress . " AND NAME LIKE '" . $search . "' ORDER BY NAME;";

	return obtenerCantidadLineas($query);

}

function obtenerAbstractsPorCongressYAuthor($idCongress, $search) {
	$query = "
	SELECT * FROM ABSTRACT ABS, ABSTRACT_AUTHOR AU, AUTHOR AUT 
	WHERE ABS.ID_ABSTRACT = AU.ID_ABSTRACT 
	AND ABS.DELETED = 0
	AND AU.ID_AUTHOR = AUT.ID_AUTHOR 
	AND AUT.ID_AUTHOR = " . $search . " 
	AND ABS.ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE S.ID_CONGRESS = " . $idCongress . ") ORDER BY TITLE";

	return obtenerListado($query);
}

function obtenerCantidadAbstractsPorCongressYAuthor($idCongress, $search) {
	$query = "
	SELECT * FROM ABSTRACT ABS, ABSTRACT_AUTHOR AU, AUTHOR AUT 
	WHERE ABS.ID_ABSTRACT = AU.ID_ABSTRACT 
	AND ABS.DELETED = 0
	AND AU.ID_AUTHOR = AUT.ID_AUTHOR 
	AND AUT.ID_AUTHOR = " . $search . " 
	AND ABS.ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE S.ID_CONGRESS = " . $idCongress . ")";

	return obtenerCantidadLineas($query);
}

function sendMail($from, $to, $subject, $body) {
	$headers = "From: " . $from . "\r\nContent-type: text/html\r\n";
	mail($to, $subject, $body, $headers);
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

function congressNameExists($congressName) {
	$query = "SELECT * FROM CONGRESS WHERE NAME = '" . $congressName . "'";
	$cantidad = obtenerCantidadLineas($query);
	return $cantidad == 1;
}

function congressNameExistsEdit($congressName, $congressId) {
	$query = "SELECT * FROM CONGRESS WHERE NAME = '" . $congressName . "' and ID_CONGRESS != " . $congressId;
	$cantidad = obtenerCantidadLineas($query);
	return $cantidad == 1;
}

function getAbstractsAdvanced($idCongress, $search1, $option1, $condition2, $search2, $option2, 
									$condition3, $search3, $option3, $condition4, $search4, $option4) {
									
	$query = "
	SELECT 
	  A.SHOW_PROCEEDING, A.ABSTRACT_KEYWORDS, A.ABSTRACT_TEXT, A.ID_ABSTRACT, A.PROCEEDING_SEARCH_KEY, A.TITLE,
	  S.NAME
	FROM 
	  ABSTRACT A,
	  SESSION S
	WHERE 
	  A.DELETED = 0 AND
	  A.ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE ID_CONGRESS = " . $idCongress . ")
	  AND A.ID_SESSION = S.ID_SESSION AND ( ";

	if (strlen($search1) > 0) {	  
	  $query = $query . $option1 . " LIKE '%" . $search1 . "%' ";
	}
	
	if (strlen($search2) > 0) {	  
	$query = $query . $condition2 . " " . $option2 . " LIKE '%". $search2 ."%' ";
	}
	
	if (strlen($search3) > 0) {	  
	$query = $query . $condition3 . " " . $option3 . " LIKE '%". $search3 ."%' ";
	}
	
	if (strlen($search4) > 0) {	  
	$query = $query . $condition4 . " " . $option4 . " LIKE '%". $search4 ."%' ";
	}

	$query = $query . " )";

	return obtenerListado($query);
}

function getAbstractsAdvancedCount($idCongress, $search1, $option1, $condition2, $search2, $option2, 
									$condition3, $search3, $option3, $condition4, $search4, $option4) {

	$query = "
	SELECT 
	  A.SHOW_PROCEEDING, A.ABSTRACT_KEYWORDS, A.ABSTRACT_TEXT, A.ID_ABSTRACT, A.PROCEEDING_SEARCH_KEY, A.TITLE,
	  S.NAME
	FROM 
	  ABSTRACT A,
	  SESSION S
	WHERE 
	  A.DELETED = 0 AND
	  A.ID_SESSION IN (SELECT S.ID_SESSION FROM SESSION S WHERE ID_CONGRESS = " . $idCongress . ")
	  AND A.ID_SESSION = S.ID_SESSION AND ( ";

	if (strlen($search1) > 0) {	  
	  $query = $query . $option1 . " LIKE '%" . $search1 . "%' ";
	}
	
	if (strlen($search2) > 0) {	  
	$query = $query . $condition2 . " " . $option2 . " LIKE '%". $search2 ."%' ";
	}
	
	if (strlen($search3) > 0) {	  
	$query = $query . $condition3 . " " . $option3 . " LIKE '%". $search3 ."%' ";
	}
	
	if (strlen($search4) > 0) {	  
	$query = $query . $condition4 . " " . $option4 . " LIKE '%". $search4 ."%' ";
	}
		
	$query = $query . " )";
		
	return obtenerCantidadLineas($query);							
}

function getAbstractById($idAbstract) {
	$query = "SELECT * FROM ABSTRACT WHERE ID_ABSTRACT = " . $idAbstract;
	return ejecutarQuery($query);
}

function getAuthorsIdsByAbstract($idAbstract) {
	$query = "SELECT ID_AUTHOR FROM ABSTRACT_AUTHOR WHERE ID_ABSTRACT = " . $idAbstract;
	return obtenerListado($query);
}

?>